//
//  AppDelegate.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/6.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        //启动程序，设置根控制器
        window?.rootViewController = {
            //启动程序，设置根控制器，是否登陆
            if WQAccountViewModel.sharedAccount.isLogin {
                //登陆成功 判断是否是新版本
                return self.isNewVersion ? WQNewFutureController() : WQWelcomeController()
            }
            //没有登陆
            return WQMainTabBarController()
        }()

        window?.makeKeyAndVisible()
        
        WQNotificationCenter.addObserverForName(WQSwitchRootViewControllerNotification, object: nil, queue: nil) { (notification) -> Void in
            
            self.window?.rootViewController = (notification.object != nil) ? WQWelcomeController() : WQMainTabBarController()
//            if notification.object != nil {
//                self.window?.rootViewController = WQWelcomeController()
//                }else{self.window?.rootViewController = WQMainTabBarController()}
        }
        
        setupAppearance()
        return true
    }
   
    deinit {
        WQNotificationCenter.removeObserver(self)
    }
    func applicationDidEnterBackground(application: UIApplication) {
        WQStatusDAL.clearDataCache()
    }
}

extension AppDelegate {
    
    private var isNewVersion:Bool {
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let preVersion = NSUserDefaults.standardUserDefaults().objectForKey("appVersion") as? String
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "appVersion")
        
        return currentVersion != preVersion
    }

    /// 设置全局外观 - 在很多应用程序中，都会在 AppDelegate 中设置所有需要控件的全局外观
    private func setupAppearance() {
        // 修改导航栏的全局外观 － 要在控件创建之前设置，一经设置全局有效
        UINavigationBar.appearance().tintColor = WBAppearanceTintColor
        UITabBar.appearance().tintColor = WBAppearanceTintColor
    }
}
